package heima3_2.cn.osc.presenter;

import java.util.ArrayList;
import java.util.List;

import heima3_2.cn.osc.model.bean.BlogBean;
import heima3_2.cn.osc.model.bean.GeneralBlogBean;
import heima3_2.cn.osc.ui.main_fragment.SyntheFragment;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/15 0015.
 */

public class SyntheBoggerPresenter extends BasePresenter<GeneralBlogBean> {
    private  SyntheFragment syntherMainFragment;
    int mFlag = 0;
    List<BlogBean> blogs = new ArrayList<>();
    public SyntheBoggerPresenter(SyntheFragment syntherMainFragment){
        this.syntherMainFragment=syntherMainFragment;
    }
    @Override
    protected void getFailureData(Throwable t) {
        t.printStackTrace();
    }

    @Override
    protected void getSuccessData(Response<GeneralBlogBean> response) {
        GeneralBlogBean body = response.body();
        if (mFlag == 0){
            blogs.removeAll(blogs);
            blogs = body.getBlogs();
            if (body.getBlogs().size() < 20) {
                syntherMainFragment.mLvSynthe.setHasMoreItems(false);
            }
        } else if (response.body().getBlogs().size() == 0){
            syntherMainFragment.mLvSynthe.setHasMoreItems(false);
        } else {
            blogs.addAll(body.getBlogs());
        }
        syntherMainFragment.mBoggerAdapter.setDatas(blogs);

        if (blogs.size() < mFlag * 20) { //如果加载到的数据不足页数乘以20，则说明已经没有数据
            syntherMainFragment.mLvSynthe.setHasMoreItems(false);
        }

//        syntherMainFragment.mBoggerAdapter.setDatas(blogs);
    }

    @Override
    public void getData(int page) {
        mFlag = page;
        Call<GeneralBlogBean> latest = mRetrofitInterface.getDetailInfo(page, 20, "latest");
        latest.enqueue(mCallback);
    }
}
